﻿/* Write a program that can solve these tasks:
Reverses the digits of a number
Calculates the average of a sequence of integers
Solves a linear equation a * x + b = 0
		Create appropriate methods.
		Provide a simple text-based menu for the user to choose which task to solve.
		Validate the input data:
The decimal number should be non-negative
The sequence should not be empty
a should not be equal to 0
*/ 

using System;
using System.Globalization;
using System.Threading;

class SolveTasks
{
    public static byte choice;
    static void Main()
    {
        Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
        do
        {
            PrintMenu();
            switch (choice)
            {
                case 1:
                    ReverseDigits();
                    break;
                case 2:
                    CalculateAverage();
                    break;
                case 3:
                    SolveLinearEquation();
                    break;
            }
        } while (choice != 0);
    }

    private static void SolveLinearEquation()
    {
        int n, b;
        bool iProvided = false;
        Console.WriteLine("Please enter a, b, and x as requested.\r\n===================");
        do
        {
            if (iProvided == true) Console.WriteLine("Error!: Invalid input. Please enter a zero-different integer number!.\r\nValid value range: a != 0; a < {1} a > {0};", Int32.MaxValue, Int32.MinValue);
            else iProvided = true;
            Console.Write("Please enter a: ");
        } while (Int32.TryParse(Console.ReadLine(), out n) == false || n == 0);
        iProvided = false;
        do
        {
            if (iProvided == true) Console.WriteLine("Error: Please enter an integer number.\r\nValid value range: {0} - {1}", Int32.MinValue, Int32.MaxValue);
            else iProvided = true;
            Console.Write("Please enter b: ");
        } while (Int32.TryParse(Console.ReadLine(), out b) == false);
        double result = ((double)-b / (double)n);
        Console.WriteLine("The result of {0}x + {1} is: {2}", n, b, result);

    }

    private static void CalculateAverage()
    {
        uint n;
        bool iProvided = false;
        do
        {
            if (iProvided == true) Console.Write("Error! Please provide a positive number!\r\nValid range: 1 - {0}", UInt32.MaxValue);
            else iProvided = true;
            Console.Write("How many numbers?: ");
        } while (UInt32.TryParse(Console.ReadLine(), out n) == false || n < 1);

        int[] nums = new int[n];
        int sum = 0;
        for (int i = 0; i < nums.Length; i++)
        {
            iProvided = false; //reset iProvided to avoid getting a "false negative" error
            do
            {
                if (iProvided == true) Console.Write("Error! Please provide an integer number!\r\n\tValid value range: {0} - {1}", UInt32.MinValue, UInt32.MaxValue);
                else iProvided = true;
                Console.Write("Enter a number: ");
            } while (Int32.TryParse(Console.ReadLine(), out nums[i]) == false);
            sum += nums[i];
        }
        Console.WriteLine("Average sum: {0:0.00}", (double)sum / (double)n);
    }

    private static void ReverseDigits()
    {
        decimal n;

        bool iProvided = false;
        do
        {
            if (iProvided == true) Console.Write("Error! Please provide a positive number!");
            else iProvided = true;
            Console.Write("Enter the number of which you want to reverse the digits: ");
        } while (decimal.TryParse(Console.ReadLine(), out n) == false || n < 0);
        string reversed_str = ReverseDigits(n);
        decimal reversed_decimal = decimal.Parse(reversed_str);
        Console.Write("Number you entered: {0}\r\nReversed number: {1}", n, reversed_decimal);
    }

    private static string ReverseDigits(decimal number)
    {
        string reversed = "";
        string straightstr = number.ToString();
        //string numberstr = 
        for (int i = straightstr.Length - 1; i >= 0; i--) reversed += straightstr[i];
        return reversed;
    }

    private static void PrintMenu()
    {

        bool iProvided = false;

        string menu = @"
Please choose an action:
        1. Reverse the digits of a number
        2. Calculates the average of a sequence of integers
        3. Solve a linear equation
        0. Exit
        ";

        do
        {
            if (iProvided == true) Console.WriteLine("Error: Invalid choice entered!");
            else iProvided = true;
            Console.Write("{0}\r\n Enter a number of your choice: ", menu);
        } while (byte.TryParse(Console.ReadLine(), out choice) == false || choice < 0 || choice > 3);
        //if (choice == 0) Environment.Exit(0);
    }
}
